<?php

/**
 * Code Controller
 *
 * Student Base Information Management
 * student entry
 *
 * @version $Id: CodeController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package default
 * @subpackage Controller
 */

class Develop_CodeController extends Zend_Controller_Action
{
    protected $_locale;
    function init()
    {
        $this->_locale = Bootstrap::$config['resources']['locale']['default'];
    }
    function indexAction()
    {

        $this->_forward('genclass');
    }



    function genclassAction()
    {
        $conn = Doctrine_Manager::connection();
        $tables = $conn->import->listTables();
        $this->view->tables = $tables;
        if ($this->getRequest()->isPost()) {

            $filters = array( '*' => 'StringTrim', '*'=>'StripTags');
            $input = new Zend_Filter_Input($filters,array(),$this->getRequest()->getParams());

            $table = $input->selTable;
            $module = $input->selModule;

            $table_name = '';
            $arr = explode("_",$table);
            foreach($arr as $val)
            $table_name .= ucfirst($val);

            $this->view->table_name = $table_name;

            $this->view->data = $this->getRequest()->getPost();
            $core = new Develop_Model_Code($module,$table);

            $this->view->code = $core->getModelTable();


        }

    }

    /**
     * 打包程式碼
     * @return unknown_type
     */
    function getzipAction()
    {
        $filters = array( '*' => 'StringTrim', '*'=>'StripTags');
        $input = new Zend_Filter_Input($filters,array(),$this->getRequest()->getParams());
        $table = $input->tableName;
        $module = $input->moduleName;
        $module_path = '';
        if ($module)
        $module_path = $module.'/';
        $table_name = '';
        $arr = explode("_",$table);
        foreach($arr as $val)
        $table_name .= ucfirst($val);

        $core = new Develop_Model_Code($module,$table);
        $zip = new Sfs_Filesystem_Zip();
        $php_tag = '<?php'."\n";
        $zip->add_file($php_tag.$core->getModelTable(),$module_path.'models/'.$table_name.".php");
        $zip->add_file($php_tag.$core->getMapper(),$module_path.'models/'.$table_name."Mapper.php");
        $zip->add_file($php_tag.$core->getDbTable(),$module_path.'models/DbTable/'.$table_name.".php");
        $zip->add_file($php_tag.$core->getForm(),$module_path.'forms/'.$table_name.".php");
        $zip->add_file($php_tag.$core->getLanguage(),$module_path.'languages/'.$this->_locale.'/Forms/'.strtolower($table_name).".php");
        $zip->send_file($table);
        exit;
    }

    /**
     * 產生程式碼
     * @return void
     */
    function getcodeAction()
    {
        //$id = $this->_request->getParam('id');
        $filters = array( '*' => 'StringTrim', '*'=>'StripTags');
        $input = new Zend_Filter_Input($filters,array(),$this->getRequest()->getParams());
        $id = $input->id;
        $table = $input->selTable;
        $module = $input->selModule;

        $core = new Develop_Model_Code($module,$table);
        switch($id) {
            case 'object':
                $this->_helper->json(array('data'=>$core->getModelTable()));
                break;
            case 'mapper':
                $this->_helper->json(array('data'=>$core->getMapper()));
                break;
            case 'dbtable' :
                $this->_helper->json(array('data'=>$core->getDbTable()));
                break;
            case 'form' :
                $this->_helper->json(array('data'=>$core->getForm()));
                break;
            case 'language':
                $this->_helper->json(array('data'=>$core->getLanguage()));
                break;
        }

    }

}
